package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.base.QueryObject;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RealAuthServiceImpl implements IRealAuthService {
    @Autowired
    private RealAuthMapper realAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    @Transactional(readOnly = true)
    public PageInfo<RealAuth> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<RealAuth> list = realAuthMapper.selectList(qo);
        return new PageInfo<RealAuth>(list);
    }

    @Override
    public RealAuth get(Long realAuthId) {
        return realAuthMapper.selectById(realAuthId);
    }

    @Override
    public void apply(RealAuth vo) {
        UserInfo current = userInfoService.getCurrent();
        // 1.判断是否已经认证通过,如果已经通过就不执行,前台可以绕过页面直接save,安全
        if (!current.getHasRealAuth()) {
            // 2.创建realAuth对象,设置相关数据,并保存
            RealAuth ra = new RealAuth();
            ra.setIdNumber(vo.getIdNumber());
            ra.setRealName(vo.getRealName());
            ra.setSex(vo.getSex());
            ra.setBornDate(new Date());
            ra.setAddress(vo.getAddress());
            ra.setState(RealAuth.STATE_NORMAL);
            ra.setImage1(vo.getImage1());
            ra.setImage2(vo.getImage2());
            ra.setRemark(vo.getRemark());
            ra.setApplyTime(new Date());
            ra.setApplier(UserContext.getCurrentUser()); // 切记申请人一定是当前登录的用户
            realAuthMapper.insert(ra);
            // 3.在userInfo对象中,更新设置realAuthId值,同时设置当前登录用户的认证相关字段
            current.setRealAuthId(ra.getId());
            userInfoService.update(current);
        }

    }

    // 实名认证审核
    @Override
    public void audit(Long realAuthId, int state, String remark) {
        // 1.查询出当前被审核的实名认证申请信息,并判断是否处于待审核
        RealAuth ra = realAuthMapper.selectById(realAuthId);

        // 查询出当前申请人的基本信息对象(根据申请人的id)
        UserInfo currentApplier = userInfoService.get(ra.getApplier().getId());
        if (!currentApplier.getHasRealAuth()) {
            // 2.给实名认证信息对象,设置相关信息,并更新
            if (ra.getState() == RealAuth.STATE_NORMAL) {
                ra.setState(state);
                ra.setRemark(remark);
                ra.setAuditor(UserContext.getCurrentUser());
                ra.setAuditTime(new Date());
                realAuthMapper.updateById(ra);
                LocalDate now = LocalDate.now();

                // 3.如果审核通过
                if (state == RealAuth.STATE_PASS) {
                    // 3.1 设置idNumber
                    // 3.2 设置realName
                    // 3.3 增加已经实名认证的状态码
                    currentApplier.setIdNumber(ra.getIdNumber())
                            .setRealName(ra.getRealName())
                            .setBitState(currentApplier.getNewState(BitStateUtil.OP_REAL_AUTH));
                } else {
                    // 4.如果审核拒绝
                    // 4.1 修改userInfo中realAuth设置为null
                    currentApplier.setRealAuthId(null);
                }
            }
            // 5.更新userInfo
            userInfoService.update(currentApplier);
        }
    }
}
